package com.example.zhinongbao.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.example.zhinongbao.R;
import com.example.zhinongbao.model.HomeModel;
import com.example.zhinongbao.model.Product;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

public class PublishProductActivity extends AppCompatActivity {

    private EditText etProductName, etProductPrice, etProductDescription;
    private Button btnSubmit;
    private ImageView ivProductImage;
    private Uri selectedImageUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_publish_product);

        // 初始化视图
        initViews();

        // 设置Toolbar
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("发货品");
    }

    private void initViews() {
        etProductName = findViewById(R.id.et_product_name);
        etProductPrice = findViewById(R.id.et_product_price);
        etProductDescription = findViewById(R.id.et_product_description);
        btnSubmit = findViewById(R.id.btn_submit);
        ivProductImage = findViewById(R.id.iv_product_image);

        // 选择图片点击事件
        ivProductImage.setOnClickListener(v -> {
            Intent intent = new Intent(Intent.ACTION_PICK);
            intent.setType("image/*");
            startActivityForResult(intent, 100);
        });

        // 提交按钮点击事件
        btnSubmit.setOnClickListener(v -> {
            if (validateInput()) {
                publishProduct();
            }
        });
    }

    private boolean validateInput() {
        String name = etProductName.getText().toString().trim();
        String priceStr = etProductPrice.getText().toString().trim();
        String description = etProductDescription.getText().toString().trim();

        if (name.isEmpty()) {
            Toast.makeText(this, "请输入商品名称", Toast.LENGTH_SHORT).show();
            return false;
        }

        if (priceStr.isEmpty()) {
            Toast.makeText(this, "请输入商品价格", Toast.LENGTH_SHORT).show();
            return false;
        }

        try {
            double price = Double.parseDouble(priceStr);
            if (price <= 0) {
                Toast.makeText(this, "价格必须大于0", Toast.LENGTH_SHORT).show();
                return false;
            }
        } catch (NumberFormatException e) {
            Toast.makeText(this, "请输入有效的价格", Toast.LENGTH_SHORT).show();
            return false;
        }

        if (description.isEmpty()) {
            Toast.makeText(this, "请输入商品描述", Toast.LENGTH_SHORT).show();
            return false;
        }

        if (selectedImageUri == null) {
            Toast.makeText(this, "请选择商品图片", Toast.LENGTH_SHORT).show();
            return false;
        }

        return true;
    }

    private void publishProduct() {
        String name = etProductName.getText().toString().trim();
        double price = Double.parseDouble(etProductPrice.getText().toString().trim());
        String description = etProductDescription.getText().toString().trim();

        // 创建新商品
        Product newProduct = new Product(
                (int) System.currentTimeMillis(), // 使用时间戳作为临时ID
                name,
                price,
                R.drawable.placeholder, // 默认图片
                description,
                "山东济南" // 默认发货地
        );

        // 添加到数据模型
        HomeModel model = new HomeModel();
        model.addProduct(newProduct);

        // 通知首页刷新 - 通过EventBus
        EventBus.getDefault().post(new ProductAddedEvent(newProduct));

        // 提示发布成功
        Toast.makeText(this, "商品发布成功", Toast.LENGTH_SHORT).show();
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == RESULT_OK && data != null) {
            selectedImageUri = data.getData();
            ivProductImage.setImageURI(selectedImageUri);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    // 商品添加事件
    public static class ProductAddedEvent {
        public final Product product;

        public ProductAddedEvent(Product product) {
            this.product = product;
        }
    }
}
